package com.vnt;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Widget;

import org.kalmeo.util.frame.Frame;
import com.ilove.core.*;
import com.ilove.frame.*;

public class IndexFrame implements Frame {

	public static IndexFrame instance = new IndexFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String code;
	public String name;
	public String[] vecContent = null;
	boolean isShort = false;
	// MainFrame's Screen
	private Screen screen;
	private final Pad pad = new Pad();
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}
	public void onAdded() {
		try {
			System.out.println("Index frame on add: " + id + name + code);
			pad.getIndexList(code,id);
			// Load the screen
			screen = Kuix.loadScreen(ILOVEContants.INDEX_SCREEN, pad);
			screen.setCurrent();
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("showMainScreen".equals(identifier)) {
			showScreen();
			return false;
		} else if ("askQuit".equals(identifier)) {
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		}
		else if ("next".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget).getDataProvider();
				if (dataprovider != null) {
					IndexList indexList = (IndexList) dataprovider;
					try {
						if(id==0){
						SubIndexFrame.instance.id = id;									System.out.println("id="+id);
						SubIndexFrame.instance.code = indexList.code;					System.out.println("SubIndexFrame.instance.code="+indexList.code);			
						Kuix.getFrameHandler().pushFrame(SubIndexFrame.instance);
						}
						else if(id==4 && indexList.code.equals("64")){
							if(name=="XOSO"){
							ContentListFrame.instance.id = id;
							ContentListFrame.instance.code = indexList.code;
							ContentListFrame.instance.name = name;
							Kuix.getFrameHandler().pushFrame(ContentListFrame.instance);
							}else if(name=="CAU"){
								ContentFrame.instance.id=4;
								ContentFrame.instance.contentName = "CAU";
								Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
							}else if(name=="SOI"){
								ContentFrame.instance.id=4;
								ContentFrame.instance.contentName = "SOI";
								Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
							}
						}else{
						SubIndexFrame.instance.id = id;
						SubIndexFrame.instance.code = indexList.code;					System.out.println("SubIndexFrame.instance.code="+indexList.code);
						SubIndexFrame.instance.name = indexList.name;					System.out.println("SubIndexFrame.instance.name="+indexList.name);
						//Danh cho Soi Cau
						SubIndexFrame.instance.code_name = name;						System.out.println("SubIndexFrame.instance.code_name="+name);
						Kuix.getFrameHandler().pushFrame(SubIndexFrame.instance);
						}
					} catch (Exception e) {
					}
				}
			}
		}
		else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} 
		else if ("about".equals(identifier)) {
			Kuix.getFrameHandler().pushFrame(AboutFrame.instance);
			return false;
		}else if ("sendsms".equals(identifier)) {
			System.out.println(ILOVE.KeyActive);
			new SendSms().Send(ILOVE.KeyActive);
			return false;
		}
		return true;
	}
	public void onRemoved() {
		try {
			if(id==4)
				com.ilove.lottery.frame.LotteryMainFrame.instance.showScreen();
			else
				com.ilove.frame.MainFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}


}